package workshopdesign.Control;
import java.util.ArrayList;
import workshopdesign.Model.*;
import java.util.HashMap;
import java.util.Calendar;
import java.util.Collection;
/**
 
 * @author (Radu) 
 * @version (09.11.10)
 */
public class LoanController
{
    // instance variables - replace the example below with your own
    private HashMap<Long, Loan> LOAN_LIST;
    private static final LoanController INSTANCE = new LoanController();
    long loanId = 0;

    
    private LoanController()
    {
        LOAN_LIST = new HashMap<Long, Loan>();
    }
    
    public static LoanController getLoanController()
    {
        return INSTANCE;
    }

    public static HashMap<Long, Loan> save(){
        return new HashMap<Long, Loan>(getLoanController().LOAN_LIST);
    }

    public static long save_id(){
        return getLoanController().loanId;
    }

    public static void load(HashMap<Long, Loan> loan_list, long next_id){
        getLoanController().LOAN_LIST = new HashMap<Long, Loan>(loan_list);
        getLoanController().loanId = next_id;
    }
    
    public void createLoan(Person person, Copy copy, int period)
    {
        Loan newLoan = new Loan(loanId, person.getId(), copy.getId(), Calendar.getInstance().getTime(), period, Loan.State.borrowed);
        LOAN_LIST.put(loanId, newLoan);
        loanId++;
    }

    public void deleteLoan(long loan_id){
        LOAN_LIST.remove(loan_id);
    }

    public Collection<Loan> get(){
        return LOAN_LIST.values();
    }

    public Loan get(long loan_id){
        return LOAN_LIST.get(loan_id);
    }
    
    public Person getPerson(long loan_id)
    {
        Loan loan = LOAN_LIST.get(loan_id);
        long person_id = loan.getPersonId();
        AddressController address_book = AddressController.getAddressController();
        return address_book.getPerson(person_id);
    }
    
    public DVD getDvd(long loan_id)
    {
        Loan loan = LOAN_LIST.get(loan_id);
        long copy_id = loan.getCopyId();
        DVDController newdvd = DVDController.getDVDControl();
        return newdvd.get_DVD_by_CopyID(copy_id);
    }
    
    public Copy getCopy(long loan_id)
    {
        Loan loan = LOAN_LIST.get(loan_id);
        long copy_id = loan.getCopyId();
        DVDController newcopy = DVDController.getDVDControl();
        return newcopy.getCopy(copy_id);
    }
    
    public void extend_loan(long id, int period)
    {
        Loan loan = LOAN_LIST.get(id);
        int new_period = loan.getPeriod();
        new_period += period;
        loan.setPeriod(new_period);
    }
    
    public void returned_copy(long id)
    {
        LOAN_LIST.get(id).setState(Loan.State.returned);
    }
    
    public void Check_expired()
    {
        long time = Calendar.getInstance().getTimeInMillis();
        for(Loan loan: LOAN_LIST.values()){
            long expire_time = loan.getExprationDate().getTime();
            if(time >= expire_time){
                loan.setState(Loan.State.expired);
            }
        }
    }

    public boolean is_copy_available(Copy copy){
        Collection<Loan> loan_list = searchCopy(copy.getId());

        for(Loan loan : loan_list){
            if(loan.getState() != Loan.State.returned){
                return false;
            }
        }
        return true;
    }
    
    public ArrayList<Loan> searchPerson(long person_id)
    {
        ArrayList<Loan> array_loan = new ArrayList<Loan>();
        for(Loan loan : LOAN_LIST.values()){
            if(loan.getPersonId() == person_id){
                array_loan.add(loan);
            }
        }
        return array_loan;
    }
    
    public ArrayList<Loan> searchDvd(long dvd_id)
    {
        ArrayList<Loan> array_loan = new ArrayList<Loan>();
        for(Loan loan : LOAN_LIST.values()){
            DVD dvd = getDvd(loan.getId());
            if(dvd.getId() == dvd_id){
                array_loan.add(loan);
            }
        }
        return array_loan;
    }
    
    public ArrayList<Loan> searchCopy(long copy_id)
    {
        ArrayList<Loan> array_loan = new ArrayList<Loan>();
        for(Loan loan : LOAN_LIST.values()){
            if(loan.getCopyId() == copy_id){
                array_loan.add(loan);
            }
        }
        return array_loan;
    }

    public void deleteLoansByCopy(long copy_id){
        Collection<Loan> loans = searchCopy(copy_id);

        for(Loan loan : loans){
            deleteLoan(loan.getId());
        }
    }

    public void deleteLoansByDVD(long dvd_id){
        Collection<Loan> loans = searchDvd(dvd_id);

        for(Loan loan : loans){
            deleteLoan(loan.getId());
        }
    }

    public void deleteLoansByPerson(long person_id){
        Collection<Loan> loans = searchPerson(person_id);

        for(Loan loan : loans){
            deleteLoan(loan.getId());
        }
    }
}
